﻿using Autofac;
using Autofac.Extras.Quartz;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zhouyu.DreamTour.WinService;
using ZhouYu.DreamTour.BLL.IService;
using ZhouYu.DreamTour.DAL.IRepository;
using ZhouYu.DreamTour.DAL.Repository;
using ZhouYu.DreamTour.Model;

namespace ZhouYu.DreamTour.WinService
{
    public class AutofacUtil
    {
        /// <summary>
        /// Autofac容器对象
        /// </summary>
        private static IContainer _container;

        /// <summary>
        /// 初始化autofac
        /// </summary>
        public static void InitAutofac()
        {
            var builder = new ContainerBuilder();

            //配置接口依赖
            builder.RegisterInstance<IDbConnection>(DBFactory.CreateConnection()).As<IDbConnection>();
            builder.RegisterType<UserRepository>().As<IUserRepository>();
            builder.RegisterType<OrderRepository>().As<IOrderRepository>();
            builder.RegisterType<PaymentRecordRepository>().As<IPaymentRecordRepository>();
            builder.RegisterType<UserRepository>().As<IUserRepository>();
            //builder.RegisterGeneric(typeof(UserRepository)).As(typeof(IUserRepository));

            //Type basetype = typeof(IDenpendecy);
            //var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList();
            //builder.RegisterAssemblyTypes(assemblys.ToArray())
            //    .Where(t => basetype.IsAssignableFrom(t) && t != basetype)
            //    .AsImplementedInterfaces().InstancePerLifetimeScope();

            //Type repository = typeof(IDenpendecyRepository);
            //builder.RegisterAssemblyTypes(assemblys.ToArray())
            //.Where(t => repository.IsAssignableFrom(t) && t != repository)
            //.AsImplementedInterfaces().InstancePerLifetimeScope();


            //注入仓储类
            //builder.RegisterAssemblyTypes(Assembly.Load("ZhouYu.DreamTour.DAL"))
            //       .Where(x => x.Name.EndsWith("Repository"))
            //       .AsImplementedInterfaces();
            //builder.RegisterAssemblyTypes(Assembly.Load("ZhouYu.DreamTour.JobScheduler"))
            //       .Where(x => x.Name.EndsWith("Job"))
            //       .AsImplementedInterfaces();




            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .AsImplementedInterfaces();
            //配置quartz.net依赖注入
            builder.RegisterModule(new QuartzAutofacFactoryModule());
            builder.RegisterModule(new QuartzAutofacJobsModule(Assembly.GetExecutingAssembly()));
            _container = builder.Build();


            //IScheduler sched = AutofacUtil.GetFromFac<IScheduler>();
            //sched.JobFactory = new AutofacJobFactorys(_container);
            //IJobDetail job = JobBuilder.Create<ExamineJob>().WithIdentity("job1", "group1").Build();
            //ITrigger trigger = TriggerBuilder.Create()
            //.WithIdentity("1JobTrigger")
            //.WithSimpleSchedule(x => x.RepeatForever()
            //.WithIntervalInSeconds(5))
            //.StartNow()
            //.Build();

            //sched.ScheduleJob(job, trigger);
            //sched.Start();
        }

        /// <summary>
        /// 从Autofac容器获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetFromFac<T>()
        {
            return _container.Resolve<T>();
        }
    }
}
